package com.stormrage.edu.serv.ent.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.ent.EntEnterpriseDao;
import com.stormrage.edu.serv.ent.EntEnterpriseServ;
import com.stormrage.entity.ent.EntEnterprise;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EntEnterpriseServImpl.java
 * @Package com.stormrage.edu.serv.ent.impl
 * @Description: 公司 Service
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Service
public class EntEnterpriseServImpl implements EntEnterpriseServ {

	private static final Logger logger = LoggerFactory.getLogger(EntEnterpriseServImpl.class);

	@Autowired
	private EntEnterpriseDao entEnterpriseDao;

	@Override
	public Integer create(EntEnterprise arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Create 】- 开始运行 ");

		// ------------------ 验证公司名称是否重复 ------------------
		Boolean b = isExist(arg0.getEnterprise());
		if (b) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "公司名称已存在", StormrageTechException.ERR_APP,
					"【 EntEnterpriseServ 】-【 Create 】");
		}

		// ------------------ 创建管理员 ------------------
		Integer index = entEnterpriseDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Create 】- 结束运行 ");
		return index;

	}

	@Override
	public void update(EntEnterprise arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Update 】- 开始运行 ");

		entEnterpriseDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 Update 】- 结束运行 ");

	}

	@Override
	public EntEnterprise queryById(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EntEnterprise entity = entEnterpriseDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 结束运行 ");
		return entity;

	}

	@Override
	public EntEnterprise queryByName(String arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", arg0);

		EntEnterprise entity = entEnterpriseDao.queryByName(map);

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryByName 】- 结束运行 ");
		return entity;

	}

	@Override
	public List<EntEnterprise> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryList 】- 开始运行 ");

		List<EntEnterprise> array = entEnterpriseDao.queryList();

		logger.debug(" 【 Serv 】- 【 EntEnterpriseServ 】- 【 QueryList 】- 结束运行 ");
		return array;

	}

	private Boolean isExist(String arg0) throws StormrageTechException {

		Boolean rs = Boolean.TRUE;

		EntEnterprise entity = queryByName(arg0);
		if (null == entity)
			rs = Boolean.FALSE;

		return rs;

	}

}
